package com.hyopt.modules.app.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.hyopt.modules.app.service.SysCustomerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName Test
 * @Description TODO
 * @Author Li9527
 * @Date 2021/4/14 23:03
 * @Version 1.0
 */
@EnableScheduling
@Component
@Slf4j
public class AutoTask {

    @Autowired
    SysCustomerInfoService customerInfoService;

    public static void main(String[] args) {
        AutoTask autoTask = new AutoTask();
        try {
            autoTask.process();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Author Li9527
     * @Description 进程自动登陆城环获取客户信息并同步
     * @Date 2022/9/8 14:39
     * @Param []
     * @return void
     **/
//    @Scheduled(cron="*/30 * * * * ? ")
    private void process() throws Exception {
        log.info("定时任务开始执行");
        String url = "http://wxxinsoft.gnway.org:11807/api/system/login";
        HttpRequest httpRequest = HttpUtil.createPost(url);
        httpRequest.body("{\"userName\":\"admin\",\"userPassword\":\"a476fc396a140c27eb178376ced085ed\"}");
        JSONObject responseJson = null;
        HttpResponse response = httpRequest.execute();
        if (response.getStatus() == 200) {
            String body = response.body();
            responseJson = JSONObject.parseObject(body);
            JSONObject jsonObject = JSONObject.parseObject(responseJson.get("data").toString());
            String token = jsonObject.get("token").toString();
            log.info("获取token成功：{}" , token);
            log.info("获取用户信息为：{}" , jsonObject.get("user"));
            customerInfoService.getZhchInfo(token);
        }else{
            log.info("获取token失败");
        }
        log.info("执行结束，数据同步完成", DateUtil.now().toString());
    }

}
